package com.evil.application.enums.reporting;

import com.evil.application.pojo.dto.reporting.ReportingTypeRespDTO;
import com.evil.common.core.enums.BaseEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 报表类型
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum ReportingTypeEnum {

    /**
     * 报表类型
     */
    DATA_TABLE(1, "数据表", new ReportingTypeRespDTO("数据表")
            .setShowPermission(true)
            .setShowFilter(true)
            .setShowDefaultSort(true)
            .setShowQuery(true)
            .setShowControlPermission(true)
            .setShowRemark(true)
    ),
    SUMMARY_TABLE(2, "汇总表", new ReportingTypeRespDTO("汇总表")
            .setShowPermission(true)
            .setShowFilter(true)
            .setShowRemark(true)
            .setReportingHeaderCount(9) // 最多9个行维度
            .setReportingColumnHeaderCount(9) // 最多9个列维度
            .setReportingTargetCount(5) // 最多5个指标
            .setShowRanking(true)
    ),
    QUOTA_CARD(3, "指标卡", new ReportingTypeRespDTO("指标卡")
            .setReportingTargetCount(1) // 最多1个指标
            .setShowFilter(true)
            .setShowRemark(true)
    ),
    COLUMNAR_CHART(4, "柱状表", new ReportingTypeRespDTO("柱状表")
            .setReportingHeaderCount(2) // 最多2个行维度
            .setReportingTargetCount(1) // 最多1个指标
            .setShowFilter(true)
            .setShowRemark(true)
            .setShowStyle(true)
    ),
    LINE_CHART(5, "折线图", new ReportingTypeRespDTO("折线图")
            .setReportingHeaderCount(2) // 最多2个行维度
            .setReportingTargetCount(1) // 最多1个指标
            .setShowFilter(true)
            .setShowRemark(true)
            .setShowStyle(true)
    ),
    PIE_CHART(6, "饼状图", new ReportingTypeRespDTO("饼状图")
            .setReportingHeaderCount(1) // 最多1个行维度
            .setReportingTargetCount(1) // 最多1个指标
            .setShowFilter(true)
            .setShowRemark(true)
            .setShowStyle(true)
    ),
    TREE_TABLE(7, "树状表", new ReportingTypeRespDTO("树状表")
            .setReportingHeaderCount(9) // 最多9个行维度
            .setReportingTargetCount(1) // 最多1个指标
            .setShowFilter(true)
            .setShowRemark(true)
            .setShowStyle(true)
    ),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;
    @Getter
    private final ReportingTypeRespDTO typeRespDTO;

    public static Optional<ReportingTypeEnum> isExistById(Integer id) {
        if (Objects.isNull(id)) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static ReportingTypeEnum findById(Integer id) {
        return ReportingTypeEnum.isExistById(id).orElseThrow(ReportingTypeEnum::getBusinessException);
    }

    public static Stream<ReportingTypeEnum> getStream() {
        return Arrays.stream(values());
    }

    public static List<ReportingTypeRespDTO> types() {
        return ReportingTypeEnum.getStream().map(e -> e.typeRespDTO).collect(Collectors.toList());
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(BaseEnum.ERROR_PARAMETER, "报表类型参数错误");
    }
}
